<template>
  <view v-if="visible" class="global-loading-overlay">
    <view class="loading-container">
      <view class="loading-spinner">
        <view class="spinner-ring"></view>
        <view class="spinner-ring"></view>
        <view class="spinner-ring"></view>
      </view>
      <text class="loading-text">{{ text }}</text>
    </view>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue'
import { loadingStore } from '@/utils/loadingStore'

// 响应式数据
const visible = ref(false)
const text = ref('加载中...')

// 监听全局加载状态
watch(
  () => loadingStore.isLoading,
  (newVal) => {
    visible.value = newVal
  },
  { immediate: true }
)

watch(
  () => loadingStore.loadingText,
  (newVal) => {
    text.value = newVal
  },
  { immediate: true }
)
</script>

<style scoped>
.global-loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.95);
  padding: 40rpx;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}

.loading-spinner {
  position: relative;
  width: 80rpx;
  height: 80rpx;
  margin-bottom: 20rpx;
}

.spinner-ring {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 4rpx solid transparent;
  border-radius: 50%;
  animation: spin 1.5s linear infinite;
}

.spinner-ring:nth-child(1) {
  border-top-color: #007aff;
  animation-delay: 0s;
}

.spinner-ring:nth-child(2) {
  border-right-color: #34c759;
  animation-delay: 0.3s;
}

.spinner-ring:nth-child(3) {
  border-bottom-color: #ff9500;
  animation-delay: 0.6s;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loading-text {
  font-size: 28rpx;
  color: #666;
  text-align: center;
}
</style>